﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Imageo.bintje;
using log4net;
using System.Threading;

namespace Imageo.ui
{
    public partial class TrailSearch2 : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        int proUserId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int proUserId = 0;
            string strProUserId = Request.QueryString["prouserid"];

            if (!string.IsNullOrEmpty(strProUserId))
            {
                if (!int.TryParse(strProUserId, out proUserId))
                {
                    Response.Redirect("~/UI/ProErrorGeneric.aspx");
                }
            }

            if (proUserId > 0)
            {
                // We have a professional user here
            }

            string searchString = Request.QueryString["searchStr"];
            spanReferenceTrail.Visible = true;

            if (!string.IsNullOrEmpty(searchString))
                spanReferenceTrail.InnerText = "Search results for " + searchString;
            else
                spanReferenceTrail.InnerText = "Search results";

            // Here we can set some additional search settings
            if (!Page.IsPostBack)
            {
                string sortField = Request.QueryString["sortby"];
                string sortAscending = Request.QueryString["asc"];

                if (!string.IsNullOrEmpty(sortField))
                {
                    Session["sortField"] = sortField;
                    Session["sortDirection"] = !string.IsNullOrEmpty(sortAscending) ? "ascending" : "descending";
                }
            }

            if (Request.QueryString["imageId"] != null || Request.QueryString["trailid"] != null)
            {
                Session["sortField"] = "similarity-score";
                Session["sortDirection"] = "descending";
            }

            Control rowSortHeaders=ListView2.FindControl("rowSortHeaders");
            Control rowTableHeaders=ListView2.FindControl("rowTableHeaders");

            if (rowSortHeaders!=null)
                rowSortHeaders.Visible = Request.QueryString["imageId"] == null && Request.QueryString["trailid"] == null;
    
            if (rowTableHeaders!=null)
                rowTableHeaders.Visible = Request.QueryString["imageId"] != null || Request.QueryString["trailid"] != null;

            trailBox.initPaths(this);
            updateIcons();
        }

        protected void updateIcons()
        {
            if ( ListView2.FindControl("imgDownTitle") == null) return;

            ListView2.FindControl("imgDownTitle").Visible = false;
            ListView2.FindControl("imgDownDescription").Visible = false;
            ListView2.FindControl("imgDownCity").Visible = false;
            ListView2.FindControl("imgDownCountry").Visible = false;
            ListView2.FindControl("imgDownLength").Visible = false;
            ListView2.FindControl("imgDownLayer").Visible = false;
            ListView2.FindControl("imgDownAddedBy").Visible = false;
            ListView2.FindControl("imgDownAddedOn").Visible = false;
            ListView2.FindControl("imgDownWaypoints").Visible = false;
            ListView2.FindControl("imgDownVisits").Visible = false;

            ListView2.FindControl("imgUpTitle").Visible = false;
            ListView2.FindControl("imgUpDescription").Visible = false;
            ListView2.FindControl("imgUpCity").Visible = false;
            ListView2.FindControl("imgUpCountry").Visible = false;
            ListView2.FindControl("imgUpLength").Visible = false;
            ListView2.FindControl("imgUpLayer").Visible = false;
            ListView2.FindControl("imgUpAddedBy").Visible = false;
            ListView2.FindControl("imgUpAddedOn").Visible = false;
            ListView2.FindControl("imgUpWaypoints").Visible = false;
            ListView2.FindControl("imgUpVisits").Visible = false;

            string sortField = Session["sortField"] as string;
            string sortDirection = Session["sortDirection"] as string;
            
            switch (sortField)
            {
                case "title":
                    ListView2.FindControl("imgDownTitle").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpTitle").Visible = sortDirection == "ascending";
                    break;
                case "description":
                    ListView2.FindControl("imgDownDescription").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpDescription").Visible = sortDirection == "ascending";
                    break;
                case "city":
                    ListView2.FindControl("imgDownCity").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpCity").Visible = sortDirection == "ascending";
                    break;
                case "country":
                    ListView2.FindControl("imgDownCountry").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpCountry").Visible = sortDirection == "ascending";
                    break;
                case "length":
                    ListView2.FindControl("imgDownLength").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpLength").Visible = sortDirection == "ascending";
                    break;
                case "layer":
                    ListView2.FindControl("imgDownLayer").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpLayer").Visible = sortDirection == "ascending";
                    break;
                case "usernick":
                    ListView2.FindControl("imgDownAddedBy").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpAddedBy").Visible = sortDirection == "ascending";
                    break;
                case "created":
                    ListView2.FindControl("imgDownAddedOn").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpAddedOn").Visible = sortDirection == "ascending";
                    break;
                case "waypoints":
                    ListView2.FindControl("imgDownWaypoints").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpWaypoints").Visible = sortDirection == "ascending";
                    break;
                case "visited":
                    ListView2.FindControl("imgDownVisits").Visible = sortDirection == "descending";
                    ListView2.FindControl("imgUpVisits").Visible = sortDirection == "ascending";
                    break;


            }
        }

        void ChangeSortSettings(string fieldName)
        {
            string tmpField = Session["sortField"] as string;
            if (tmpField == fieldName)
            {
                string tmpDir = Session["sortDirection"] as string;
                if (tmpDir == "ascending")
                    Session["sortDirection"] = "descending";
                else
                    Session["sortDirection"] = "ascending";
            }
            else
            {
                Session["sortField"] = fieldName;
                Session["sortDirection"] = "ascending";
            }
            updateIcons();
        }

        protected void lnkSortByTitle_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("title");
        }
        protected void lnkSortByDescription_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("description");
        }
        protected void lnkSortByCity_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("city");
        }
        protected void lnkSortByCountry_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("country");
        }
        protected void lnkSortByLength_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("length");
        }
        protected void lnkSortByLayer_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("layer");
        }
        protected void lnkSortByAddedBy_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("usernick");
        }
        protected void lnkSortByAddedOn_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("created");
        }
        protected void lnkSortByWaypoints_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("waypoints");
        }
        protected void lnkSortByVisits_Click(object sender, EventArgs e)
        {
            ChangeSortSettings("visited");
        }

    }
}
